<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Model;

class GuidController extends Controller {
    public function index(){
        
    }
    
   
    public function guid_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Guid')->getCata($type);// 得到一级分类下面的二级分类列表
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result['list']);
            $this->assign('page',$result['show']);
            $this->display('Guid/Guid_Cata_list');
        }
    
    }
    
    public function Guid_list(){
        
        if(IS_GET){
            $type=I('get.cat_two');
            if($type!=''){
                $result=D('Guid')->guid_list($type);
                $this->assign('type',$type);
                $this->assign('guid_List',$result['list']);
                $this->assign('page',$result['show']);
                $this->display('Guid/Guid_list');
            }
        }
    }
    
    /*
     * 得到分类选择中的一级分类。
     */
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    
    public function add_guid(){
        if(I('get.id')==''){                       //添加指南
        $level_two=I('get.type');
        $this->assign('level_two',$level_two);
        $this->display('Guid/add');
        }
        else{
          $map['id']=I('get.id');                                          //修改界面
          $result=M('Guid')->where($map)->select();
          $level_two=I('get.type');
          $this->assign('level_two',$level_two);
          $this->assign('id',I('get.id'));
          $this->assign('Guid_edit',$result);
          $this->display('Guid/edit');
        }
    }
    
    public function save(){
        if(I('post.id')==''){                       //添加指南
            $data['cata_two']=I('post.cata_two');
            $data['title']=I('post.title');
            $data['content']=I('post.content');
            $data['date']=time();
            $result=M('Guid')->data($data)->add();
            $result>=0?$this->success('添加指南成功',U('Guid_list',['cat_two'=>$data['cata_two']])):$this->error('添加指南失败');
        }
        else{
             $data['title']=I('post.title');
             $data['content']=I('post.content');
             $data['cata_two']=I('post.cata_two');
             $map['id']=I('post.id');
             $result=M('Guid')->where($map)->data($data)->save();
             $result>=0?$this->success('修改指南成功',U('Guid_list',['cat_two'=>$data['cata_two']])):$this->error('修改指南失败');
        }
    }
    
    public function delete_single(){
        if(IS_GET){
            $map['id']=I('get.id');
            $result=M('Guid')->where($map)->delete();
            $result?$this->success('删除成功'):$this->error('删除失败');
        }
    }
    
    public function delete_all(){
        if(IS_GET){
            $info=I('get.');
            if(!empty($info)){
                $re=explode('@',$info['ids']);
                if(!empty($re)){
                    $map['id']=array('in',$re);
                    $result=M('Guid')->where($map)->delete();
                    $result=$result?true:false;
                    $this->ajaxReturn($result);
                }
                else{
                    $this->ajaxReturn(false);
                }
            }else{
                    $this->ajaxReturn('提交信息出错');
            }
            }
          }
          
    
}